/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Nhavien;
import pojo.Phieunhaphang;
import util.HibernateUtil;

/**
 *
 * @author FT
 */
public class PhieuNhapHangDAO {

    public static List<Phieunhaphang> layDanhSachPhieuNhapHang() {
        List<Phieunhaphang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select sv from Phieunhaphang sv";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static String MaTangTuDong() {
        String chuoi = "";
        String macuoicung = "";
        List<Phieunhaphang> listpnh = PhieuNhapHangDAO.layDanhSachPhieuNhapHang();
        if (PhieuNhapHangDAO.layDanhSachPhieuNhapHang().size() == 0) {
                chuoi = "pnh001";
                return chuoi;
        }
        macuoicung += listpnh.get(PhieuNhapHangDAO.layDanhSachPhieuNhapHang().size() - 1).getIdphieunhaphang();
        String index = macuoicung.substring(3);
        try {
            
                //System.out.println("Index la"+index+"");
                int chiso = Integer.valueOf(index);
                chiso++;

                if (chiso < 10) {
                    chuoi += "pnh00" + chiso;
                } else if (chiso >= 10 && chiso < 100) {
                    chuoi += "pnh0" + chiso;
                } else if (chiso >= 100) {
                    chuoi += "pnh" + chiso;
                }
            
        } catch (Exception e) {
            chuoi += "NA";
        }
        return chuoi;
    }

    public static void Insert(String maphieu, Date ngaynhap, String manhanvien,int tinhtrang) {
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {
            session.beginTransaction();
            Phieunhaphang pnh = new Phieunhaphang(maphieu);
            pnh.setNgaynhap(ngaynhap);
            Nhavien nv = new Nhavien(manhanvien);
            pnh.setNhavien(nv);
            pnh.setTinhtrang(tinhtrang);
            session.saveOrUpdate(pnh);
            session.getTransaction().commit();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
    }
    
    public static Phieunhaphang GetPhieuNhapHang(String ids){
        Phieunhaphang pn = new Phieunhaphang();
        List<Phieunhaphang> listpnh = PhieuNhapHangDAO.layDanhSachPhieuNhapHang();
        System.out.println("So luong: "+listpnh.size());
        for (int i=0;i<listpnh.size();i++){
            if (ids.equals(listpnh.get(i).getIdphieunhaphang()))
                System.out.println("Ma: "+listpnh.get(i).getIdphieunhaphang());
                pn = listpnh.get(i);
        }
        return pn;
    }
    public static void Xoa(String maphieu){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Phieunhaphang pn = new Phieunhaphang();
        pn = PhieuNhapHangDAO.GetPhieuNhapHang(maphieu);
        try {
            session.beginTransaction();
            pn.setTinhtrang(2);
            session.saveOrUpdate(pn);
            session.getTransaction().commit();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
    }
    
    public static void SetNgayCapNhat(String maphieu,Date date){
         Session session = HibernateUtil.getSessionFactory().openSession();
         Phieunhaphang pnh = new Phieunhaphang(maphieu);
          try {
            session.beginTransaction();
            pnh.setNgaycapnhat(date);
            session.saveOrUpdate(pnh);
            session.getTransaction().commit();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
    }
    
    public static void Insert2(String maphieu, Date ngaynhap, String manhanvien,int tinhtrang, Date ngaycapnhat) {
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {
            session.beginTransaction();
            Phieunhaphang pnh = new Phieunhaphang(maphieu);
            pnh.setNgaynhap(ngaynhap);
            Nhavien nv = new Nhavien(manhanvien);
            pnh.setNhavien(nv);
            pnh.setTinhtrang(tinhtrang);
            pnh.setNgaycapnhat(ngaycapnhat);
            session.saveOrUpdate(pnh);
            session.getTransaction().commit();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
    }
}
